<?php
include('../connect.php');
session_start();

function checkValues($value){
	$value = trim($value);
	if(get_magic_quotes_gpc()){
		$value = stripslashes($value);
		}
	$value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
	$value = strip_tags($value);
	$value = mysql_real_escape_string($value);
	$value = htmlspecialchars($value);
	return $value;
	}
	
	if(checkValues($_GET['comment_text']) && $_GET['post_id'])
	{
		$userip = $_SERVER['REMOTE_ADDR'];
		
		mysql_query("INSERT INTO comments (com_msg,post_id_fk,uid_fk,ip,created,xcreated) 
		VALUES('".checkValues($_GET['comment_text'])."','".$_GET['post_id']."','".$_SESSION['uid']."','".$userip."','".date("Y-m-d H:i:s")."','".strtotime(date("Y-m-d H:i:s"))."')");
		
		$result = mysql_query("SELECT *,
		UNIX_TIMESTAMP() - xcreated AS CommentTimeSpent FROM comments order by com_id desc limit 1");
	}

	function clickable_link($text = '')
	{
		$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
		$ret = ' ' . $text;
		$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
		
		$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
		$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
		$ret = substr($ret, 1);
		return $ret;
	}
	
	while($rows = mysql_fetch_array($result)){
		
		$days = floor($rows['CommentTimeSpent']/(60*60*24));
		$remainder = $rows['CommentTimeSpent']%(60*60*24);
		$hours = floor($remainder/(60*60));
		$remainder = $remainder%(60*60);
		$minutes = floor($remainder/60);
		$seconds = $remainder%60;
		
		if($days == 0 && $hours == 0 && $minutes == 0){
			$when = 'A few seconds ago ';
		}elseif($days == 0 && $hours == 0){
			if($minutes==1){
			$when = 'About a minute ago ';	
			}else{
			$when = $minutes.'minutes ago '; }
		}elseif($days == 0){
			if($hours == 1){
			$when = 'About an hour ago ';
			}else{
			$when = $hours.' hours ago '; }
		}elseif($days > 0){
			$when = date('F d Y',$rows['xcreated']);
		}else{}
		
	$cname = $_SESSION['firstname'].' '.$_SESSION['lastname'];
		
?>
	<div class="" id="record-<?php echo $rows['com_id'];?>" style="width:450px; min-height:40px; border-top:#0F9 solid 1px; padding:2px 0 2px 10px; background-color:#E1FD8D; overflow:auto;">
        <div class="comt_pic" style="float:left; width:50px; min-height:40px; margin-top:1px;">
            <img class="comt_pic" src="<?php echo '../prof_pics/40size'.$_SESSION['uid'].'_propic.png' ?>" />
        </div>
		<div class="comt_msg" style="float:right; min-height:40px; width:390px;">
			<a href="" class="green" style=" font-weight:bold; font-size:13px;">
                <?php echo $cname; ?></a>
                        <span style=" font-size:13px;"><?php echo $rows['com_msg']; ?></span>
					<br /><wbr />
                    <div class="date" style="font:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; font-size:11px; color:#999999;">
					&nbsp; &nbsp; &nbsp;<?php echo $when; ?> &middot;
                        <?php if($uid == $rows['uid_fk']){ ?>
						<a href="#" id="CID-<?php echo $rows['com_id']; ?>" class="c_delete">Delete</a>
						<?php }?>
                    </div>						
				
			</div>
    	
	</div>
    
    <hr style="color:#FFFFFF; size:2px;"/>
    
<? } ?>